#light
namespace FSharp.WebTools.ClientControls

open System
open System.Web
open System.Web.UI

open FSharp.WebTools
open FSharp.WebTools.Core
open FSharp.WebTools.ClientLibrary
open FSharp.WebTools.ClientControls

(*
Dom.FloatWindow.createInstance = function(id) {
  var el = document.createElement("DIV");
  el.id = id;
  el.style.visibility = "hidden";
  el.style.border = "solid 1px black";
  el.style.background = "#808080";
  el.style.position = "absolute";
  var str = ('  <div style="background:#e0e0ff;height:20px;" id="{0}_header">' +
    '    <a href="javascript:;" style="float:right;" id="{0}_close">X</a>' +
    '    <span id="{0}_title"></span>' +
    '  </div>' +
    '  <div id="{0}_content" style="background:white;"></div>');
  while(str.indexOf('{0}') != -1) str = str.replace('{0}', id);
  el.innerHTML = str;
  return new Dom.FloatWindow(el);
}
*)
[<MixedSide; ParseChildren(false)>]
type FloatWindow = class
  inherit ClientControl as base
  interface INamingContainer

  val mutable header : Label
  val mutable title : Label
  val mutable close : HyperLink
  val mutable content : Element 
  
  [<DuplexField>]
  val mutable wsize : int*int
    
  new() = 
    let fnull() = (Array.zero_create 1).[0]
    { header = fnull(); content = fnull(); 
      title = fnull(); close = fnull(); 
      
      wsize=(350,300); }
  
  override this.OnInit(e) = 
    this.close <- new HyperLink();
    this.close.ID <- "close";
    this.Controls.Add(this.close);
    this.header <- new Label();
    this.header.ID <- "header";
    this.Controls.Add(this.header);
    this.content <- new Element();
    this.content.ID <- "content";
    this.Controls.Add(this.content);
    this.title <- new Label();
    this.title.ID <- "title";
    this.Controls.Add(this.title);
    base.OnInit(e)

  [<ReflectedDefinition>]
  member this.Title 
    with get() = duplex { return this.title.ClientText }
    and set(v) = duplex { do! this.title.set_ClientText(v) }
  
  [<ReflectedDefinition>]
  member this.WindowSize
    with get() = duplex { return this.wsize }
    and set(v) = 
      makeDuplex
        (server { do this.wsize <- v })
        (client 
          { do this.wsize <- v; 
            do! this.Show() })
    
  [<ReflectedDefinition>]
  member this.Client_Load ((sender:Object),(e:EventArgs)) =
    client
      { do this.close.ClientVisible <- false;
           this.header.SetupDragDrop(this);
           this.close.ClientClick.AddClient(this.CloseClicked); }
    
  [<ReflectedDefinition>]
  member this.CloseClicked ((sender:obj),(e:CancelEventArgs)) =
    client
      { do e.Canceled <- true;
           this.close.ClientVisible <- false;
           this.ClientVisible <- false; }
  
  [<ReflectedDefinition>]
  member this.Show () =
    client
      { let sz = new ClientLibrary.Size()
        let (w,h) = this.wsize
        do  sz.Width <- w;
            sz.Height <- h;
            (this :> IElement).Size <- sz;
            this.close.ClientVisible <- true;
            this.ClientVisible <- true; }
    
  override this.Render(wr) =
    wr.WriteLine(sprintf "<div id=\"%s\" style=\"visibility:hidden; position:absolute;\" class=\"%s\">" this.ClientID this.CssClass);
    wr.WriteLine("<div class=\"fwheader\" id=\"{0}_header\">", this.ClientID);
    wr.WriteLine("  <a href=\"javascript:;\" style=\"float:right;\" id=\"{0}_close\">X</a>", this.ClientID);
    wr.WriteLine("  <span id=\"{0}_title\">{1}</span>", this.ClientID, execute(this.title.ClientText));
    wr.WriteLine("</div>");
    wr.WriteLine("<div id=\"{0}_content\" class=\"fwcont\">", this.ClientID);
    for x in this.Controls do
      if (x <> (this.title:>Control) && x <> (this.close:>Control) && x <> (this.header:>Control) && x <> (this.content:>Control)) then
        x.RenderControl(wr)
    wr.WriteLine("</div></div>", this.ClientID);
    
end